#include <windows.h>
#include <deque>


template <class T> class concurrent_queue {
volatile long usedflg;
std::deque<T>  vec;
unsigned int sz;
public:
concurrent_queue(){  Initialize(); }
void Initialize(){ vec.clear(); usedflg = FALSE; sz=0; }
unsigned int size() { return sz; }
bool try_pop(T &x) {
if(sz==0) return 0;
if ( InterlockedCompareExchange ( &usedflg, TRUE, FALSE) == TRUE )  return 0;
x = vec.front(); vec.pop_front();
sz--;
InterlockedExchange( &usedflg, FALSE );
return 1; }
//
bool try_push(const T &x) {
if ( InterlockedCompareExchange ( &usedflg, TRUE, FALSE) == TRUE )  return 0;
vec.push_back( x );
sz++;
InterlockedExchange( &usedflg, FALSE );
return 1; }
void push(const T &x) { while( !try_push(x) ) Sleep(1); }
};
